package com.zzh.myapplication.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import net.icarplus.car.R;
import net.icarplus.car.activity.CertificateUploadActivity;
import net.icarplus.car.activity.CouponActiviity;
import net.icarplus.car.activity.Invoiceactivity;
import net.icarplus.car.activity.MainActivity;
import net.icarplus.car.activity.MicroEnergyActivity;
import net.icarplus.car.activity.PersonMsgActivity;
import net.icarplus.car.activity.QuitActivity;
import net.icarplus.car.activity.RedPacketCarActivity;
import net.icarplus.car.activity.RemainingSumActivity;
import net.icarplus.car.activity.SettingActivity;
import net.icarplus.car.app.MyApplication;
import net.icarplus.car.bean.AcctResultInfo;
import net.icarplus.car.constant.UrlValues;
import net.icarplus.car.manager.BaseFragment;
import net.icarplus.car.manager.MainSelect;
import net.icarplus.car.service.PublicRequest;
import net.icarplus.car.tools.LoadingUtil;
import net.icarplus.car.tools.PromptDialog;
import net.icarplus.car.tools.SPUtils;
import net.icarplus.car.tools.UserUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 项目名称：icarplus-car-android
 * 类描述：
 * 创建人：Gophy
 * 创建时间：16/3/3 11:54
 * 修改人：Gophy
 * 修改时间：16/3/3 11:54
 * 修改备注：
 */
public class MineFragment extends BaseFragment {
    private LinearLayout mine_certificate_lilayout;
    private LinearLayout remaining_sum_lilayout;
    private LinearLayout mine_recharge_lilayout;
    private LinearLayout mine_acct_point_lilayout;
    private LinearLayout quit_layout;
    private LinearLayout mine_setting_lilayout;
    private LinearLayout micro_energy;
    private LinearLayout notification_message;
    private Button btnLogOut;

    private SimpleDraweeView mine_head_imagv;

    private LoadingUtil loadingUtil;

    private TextView phoneTxt, acctMoneyTxt, couponTxt,
            certificateVerifyTxt;

    private String tbalance;
    private String frozenbal;

    @Override
    public void getsavedInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_mine, null);
    }

    @Override
    public void initComponent(View view) {
        mine_certificate_lilayout = (LinearLayout) view.findViewById(R.id.mine_certificate_lilayout);
        remaining_sum_lilayout = (LinearLayout) view.findViewById(R.id.remaining_sum_lilayout);
        mine_recharge_lilayout = (LinearLayout) view.findViewById(R.id.mine_recharge_lilayout);
        mine_acct_point_lilayout = (LinearLayout) view.findViewById(R.id.mine_acct_point_lilayout);
        quit_layout = (LinearLayout) view.findViewById(R.id.quit);
        micro_energy = (LinearLayout) view.findViewById(R.id.micro_energy);
        mine_setting_lilayout = (LinearLayout) view.findViewById(R.id.mine_setting_lilayout);
        btnLogOut = (Button) view.findViewById(R.id.btn_log_out);
        loadingUtil = new LoadingUtil(getActivity(), getString(R.string.loading_text));
        notification_message = (LinearLayout) view.findViewById(R.id.notification_message);
        mine_head_imagv = (SimpleDraweeView) view.findViewById(R.id.mine_head_imagv);

        phoneTxt = (TextView) view.findViewById(R.id.mine_phone_txt);
        acctMoneyTxt = (TextView) view.findViewById(R.id.mine_acct_money_txt);

        certificateVerifyTxt = (TextView) view
                .findViewById(R.id.mine_certificate_verify_txt);
        couponTxt = (TextView) view.findViewById(R.id.mine_coupon_txt);
    }

    @Override
    public void setListener() {
        mine_certificate_lilayout.setOnClickListener(this);
        remaining_sum_lilayout.setOnClickListener(this);
        mine_recharge_lilayout.setOnClickListener(this);
        mine_acct_point_lilayout.setOnClickListener(this);
        mine_setting_lilayout.setOnClickListener(this);
        notification_message.setOnClickListener(this);
        mine_head_imagv.setOnClickListener(this);
        micro_energy.setOnClickListener(this);
        quit_layout.setOnClickListener(this);
        if (!SPUtils.getBoolean(getActivity(), "isLogin", false)) {
            btnLogOut.setVisibility(View.GONE);
        } else {
            btnLogOut.setVisibility(View.VISIBLE);
            btnLogOut.setOnClickListener(this);
        }

    }

    @Override
    public void onAfterInit() {


//        PublicRequest.customMainRequest(getActivity(), UrlValues.QUERY_ACCOUNT_INTO_URL, handler, json);

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.mine_certificate_lilayout:
                intent.setClass(getActivity(), CertificateUploadActivity.class);
                start_Activity(intent);
                break;
            case R.id.btn_log_out:
                showAlerDialog("温馨提示", "确定退出登录吗?", new PromptDialog.OnClickListener() {
                    @Override
                    public void onClick(Dialog dialog, int which) {
                        UserUtils.cleanUserInfo(getActivity());
                        Intent logOutIntent = new Intent(getActivity(), MainActivity.class);
                        MainActivity.mainPage = MainSelect.MAIN;
                        start_Activity(logOutIntent);
                    }
                }, null);
                break;
            case R.id.remaining_sum_lilayout:
                Intent RemainIntent = new Intent(getActivity(), RemainingSumActivity.class);
                intent.putExtra("tBalance", tbalance);

                start_Activity(RemainIntent);
                break;
            case R.id.quit:
                Intent quitintent = new Intent(getActivity(), QuitActivity.class);
                start_Activity(quitintent);
                break;
            case R.id.mine_recharge_lilayout:
                Intent redpacket = new Intent(getActivity(), RedPacketCarActivity.class);
                start_Activity(redpacket);
                break;
            case R.id.mine_acct_point_lilayout:
                Intent coupon = new Intent(getActivity(), CouponActiviity.class);
                start_Activity(coupon);
                break;
            case R.id.mine_setting_lilayout:
                Intent setting = new Intent(getActivity(), SettingActivity.class);
                start_Activity(setting);
                break;
            case R.id.micro_energy:
                Intent microenergy = new Intent(getActivity(), MicroEnergyActivity.class);
                start_Activity(microenergy);
                break;
            case R.id.mine_head_imagv:
                Intent personMsgIntent = new Intent(getActivity(), PersonMsgActivity.class);
                start_Activity(personMsgIntent);
                break;
            case R.id.notification_message:
                Intent intent1 = new Intent(getActivity(), Invoiceactivity.class);
                start_Activity(intent1);
                break;
            default:
                break;
        }
    }

    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 1000:
                    MineFragment.this.loadingUtil.stopShowLoading();
                    Log.e("1000", msg.obj.toString());
                    refreshUI(MyApplication.acctResultInfo);

                    break;
                case 3000:
                    Log.e("3000", "3000000");
                    break;

                default:
                    break;
            }
        }
    };

    private void refreshUI(AcctResultInfo acctResultInfo) {
        if (MyApplication.acctResultInfo != null) {

            String headUrl = UrlValues.CAR_IMG_ID + acctResultInfo.headImgUrl;

//            ImageCacheManager.setImageBackgroundToImageView(getActivity(),
//                    mine_head_imagv, headUrl, R.mipmap.icon_mine_default_person);

            mine_head_imagv.setImageURI(Uri.parse(headUrl));
            String mobile = acctResultInfo.mobile;
            //余额
            tbalance = acctResultInfo.tBalance;
            // 冻结金额
            frozenbal = acctResultInfo.frozenBal;
            String substring = mobile.substring(0, 3);
            String substring2 = mobile.substring((mobile.length() - 3),
                    mobile.length());
            mobile = substring + "*****" + substring2;
            phoneTxt.setText(mobile);

            acctMoneyTxt.setText(acctResultInfo.tBalance);
            couponTxt.setText(acctResultInfo.couponNum);
            String rentCertStatus = acctResultInfo.rentCertStatus;
            if (rentCertStatus == null) {
                certificateVerifyTxt.setText("");
            } else if (rentCertStatus.equals("01")) {
                certificateVerifyTxt.setText("未验证");
                certificateVerifyTxt.setVisibility(View.VISIBLE);
            } else if (rentCertStatus.equals("02")) {
                certificateVerifyTxt.setText("待审核");
                certificateVerifyTxt.setVisibility(View.VISIBLE);
            } else if (rentCertStatus.equals("03")) {
                certificateVerifyTxt.setVisibility(View.INVISIBLE);
            } else if (rentCertStatus.equals("04")) {
                certificateVerifyTxt.setText("审核未通过");
                certificateVerifyTxt.setVisibility(View.VISIBLE);
            } else {
                certificateVerifyTxt.setText("");
                certificateVerifyTxt.setVisibility(View.INVISIBLE);
            }


        }
    }

    @Override
    public void onResume() {
        super.onResume();
        String mobile = SPUtils.getString(getActivity(), "LoginInfo", "");
        JSONObject json = new JSONObject();
        try {
            json.put("mobile", mobile);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        this.loadingUtil.startShowLoading();

        PublicRequest.queryUserInfo(getActivity(), handler, mobile);
    }
}
